package com.yunyan.config;

import com.yunyan.util.JwtUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class WebInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        String requestURI = request.getRequestURI();
        if (requestURI.endsWith("login")
                || requestURI.endsWith("logout")
                || requestURI.endsWith("register")
                || requestURI.contains("load")
           //     || requestURI.contains("predict")
        ) {
            return true;
        }
        if (!StringUtils.isEmpty(token)) {
            token = token.replace("Bearer ", "");
            String userId = JwtUtil.decodeToken(token);
            if (userId != null) {
                return true;
            }
        }
        throw new BusinessException("NO LOGIN");
    }
}
